#include <QtCore>
#include <QtNetwork>
#include "httpgetter.h"
#include <QImage>
#include <iostream>

HttpGetter::HttpGetter(QObject *parent)
    : QObject(parent)
{
}


void HttpGetter::httpFinished()
{
    QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    if (reply->error()) {
        std::cerr << "Download failed: " << std::endl;
    }

    reply->deleteLater();
    reply = 0;

    emit dataDownloaded();
}

void HttpGetter::httpReadyRead()
{
    imgData += reply->readAll();
}
QByteArray HttpGetter::getImgData() const
{
    return imgData;
}

void HttpGetter::setImgData(const QByteArray &value)
{
    imgData = value;
}


void HttpGetter::startRequest(const QUrl &url)
{
    reply = qnam.get(QNetworkRequest(url));
    connect(reply, SIGNAL(finished()),
            this, SLOT(httpFinished()));
    connect(reply, SIGNAL(readyRead()),
            this, SLOT(httpReadyRead()));
}
